package com.teamwin.hdmi.donate;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

@SuppressWarnings("unchecked")
public class DisplayOptions extends SocketManager {
	
	class ButtonListener implements View.OnClickListener {
		DisplayOptions parent;
		
		public ButtonListener(DisplayOptions parent) {
			this.parent = parent;
		}
		
		@Override
		public void onClick(View v) {
			this.parent.checkSocket();
			if (this.parent.sendCommand(SocketManager.GET_MIRROR_STATUS, null).equalsIgnoreCase("1")) {
				if (!this.parent.isBannersEnabled()) {
					this.parent.sendCommand(SocketManager.SET_BANNER, null);
					
					Button b = (Button) findViewById(R.id.disableBanners);
					b.setText("Disable Banners");
					ImageView image = (ImageView) findViewById(R.id.banner_image_status);
			        image.setImageResource(R.drawable.bannerstatusenable);
			        
			        this.parent.getEditor().putBoolean("banners_enabled", true);
			        this.parent.getEditor().commit();
				} else {
					this.parent.sendCommand(SocketManager.SET_BANNER, "0");
					Button b = (Button) findViewById(R.id.disableBanners);
					b.setText("Enable Banners");
					ImageView image = (ImageView) findViewById(R.id.banner_image_status);
			        image.setImageResource(R.drawable.bannerstatusdisable);
			        
			        this.parent.getEditor().putBoolean("banners_enabled", false);
			        this.parent.getEditor().commit();
				}
				this.parent.setBannersEnabled(!this.parent.isBannersEnabled());
			} else {
				Toast.makeText(this.parent, "Error: Cannot change banners while not mirroring!", Toast.LENGTH_SHORT).show();
			}
		}
	}
	
	class CheckBoxListener implements View.OnClickListener {
		DisplayOptions parent;
		
		public CheckBoxListener(DisplayOptions parent) {
			this.parent = parent;
		}
		
		@Override
		public void onClick(View v) {
			CheckBox checkBox = (CheckBox) findViewById(R.id.manual_resolution);
	        
	        this.parent.setManualResolution(checkBox.isChecked());
	        this.parent.populateResolutions(this.parent.useManualResolution());
	        this.parent.getEditor().putBoolean("manual_resolution", this.parent.useManualResolution());
	        this.parent.getEditor().commit();
		}
	}
	
	public class SeekListener implements SeekBar.OnSeekBarChangeListener {
		DisplayOptions parent;
		
		public SeekListener(DisplayOptions parent) {
			this.parent = parent;
		}
		
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			double dProgress = (double)progress;
			double max = 255;
			double percent = (dProgress / max) * 100;
			int iPercent = (int)percent;
			
			String label = "Adjust Overscan: " + iPercent + "%";
			TextView tv = (TextView) this.parent.findViewById(R.id.overscanLabel);
			tv.setText(label);
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
//			System.out.println("Seekbar progress: " + seekBar.getProgress());
			this.parent.getEditor().putString("overscan", seekBar.getProgress() + "");
			this.parent.getEditor().commit();
	        this.parent.sendCommand(SET_OVERSCAN, seekBar.getProgress() + "");
		}
	}
	
	public class ResolutionSelectionListener implements OnItemSelectedListener {
		DisplayOptions parent = null;
		
		public ResolutionSelectionListener(DisplayOptions parent) {
			this.parent = parent;
		}
		
	    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
	    	String resolution = parent.getItemAtPosition(pos).toString();
	    	
	        if (pos != this.parent.getSelectedResolution()) {
	        	this.parent.getEditor().putInt("selected_resolution", pos);
		        this.parent.getEditor().commit();
		        if (this.parent.sendCommand(GET_MIRROR_STATUS, null).equalsIgnoreCase("1")) {
		        	this.parent.sendCommand(DISABLE, null);
		        	this.parent.sendCommand(SET_RESOLUTION, resolution);
			    	this.parent.sendCommand(ENABLE, null);
		        } else {
		        	this.parent.sendCommand(SET_RESOLUTION, resolution);
		        }
		        this.parent.setSelectedResolution(pos);
//		        System.out.println("NEW Selection changed: " + pos + ", rez = " + resolution);
	        }
	    }

		public void onNothingSelected(AdapterView parent) {
	      // Do nothing.
	    }
	}
	
	public class ScalingSelectionListener implements OnItemSelectedListener {
		DisplayOptions parent = null;
		
		public ScalingSelectionListener(DisplayOptions parent) {
			this.parent = parent;
		}
		
	    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {	
	        this.parent.getEditor().putInt("scaling", pos);
	        this.parent.getEditor().commit();
	        
	        if (pos != this.parent.getScaling()) {
		        this.parent.sendCommand(SET_SCALING, pos + "");
	        }
	        this.parent.setScaling(pos);
	    }

		public void onNothingSelected(AdapterView parent) {
	      // Do nothing.
	    }
	}
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.display_options);
		
		this.setSelectedResolution(this.getSettings().getInt("selected_resolution", 0));
		
		this.checkBanners();
		
		if (!this.sendCommand(GET_MIRROR_STATUS, null).equalsIgnoreCase("1")) {
			Button button = (Button) findViewById(R.id.disableBanners);
			button.setEnabled(false);
		}
		
		final SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar1);
		SeekListener seekListener = new SeekListener(this);
		seekBar.setOnSeekBarChangeListener(seekListener);
		
		String overscan = this.getSettings().getString("overscan", "");
		if (!overscan.equalsIgnoreCase("")) {
			seekBar.setProgress(Integer.parseInt(overscan));
			
			double dProgress = Double.parseDouble(overscan);
			double max = 255;
			double percent = (dProgress / max) * 100;
			int iPercent = (int)percent;
			
			String label = "Adjust Overscan: " + iPercent + "%";
			TextView tv = (TextView) this.findViewById(R.id.overscanLabel);
			tv.setText(label);
		}
	    
		final Button bannerButton = (Button) findViewById(R.id.disableBanners);
		ButtonListener listener = new ButtonListener(this);
		bannerButton.setOnClickListener(listener);
		
		final CheckBox checkBox = (CheckBox) findViewById(R.id.manual_resolution);
		CheckBoxListener checkListener = new CheckBoxListener(this);
		checkBox.setOnClickListener(checkListener);
		
		this.setCheckBox();
		
		this.populateResolutions(this.useManualResolution());
		
		Spinner resolutionSpinner = (Spinner) findViewById(R.id.resolutions);
		resolutionSpinner.setOnItemSelectedListener(new ResolutionSelectionListener(this));
		
		Spinner scalingSpinner = (Spinner) findViewById(R.id.scalingOptions);
		scalingSpinner.setOnItemSelectedListener(new ScalingSelectionListener(this));
		
		this.checkScaling();
	}
	
	protected void onPause() {
        super.onPause();
	}

	public void checkScaling() {
		Spinner spinner = (Spinner) findViewById(R.id.scalingOptions);
	    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
	            this, R.array.scalingArray, android.R.layout.simple_spinner_item);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spinner.setAdapter(adapter);
	    
		int scaling = this.getSettings().getInt("scaling", 1);
		spinner.setSelection(scaling);
	}
	
	public void populateResolutions(boolean manual) {
		ArrayList<String> list = new ArrayList<String>();
		String resolutions = this.sendCommand(GET_RESOLUTIONS_SUPPORTED, null).substring(1);
	    String[] split = resolutions.split(",");

		for (String rez : split) {
			if (!rez.equalsIgnoreCase("0x0")) {
//				System.out.println(rez);
				list.add(rez);
			}
		}
	    
		Spinner spinner = (Spinner) findViewById(R.id.resolutions);
		ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, list);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		
//		System.out.println("Setting selection to " + this.getSettings().getInt("selected_resolution", 0) + ", adapter count = " + adapter.getCount());
		int selectedIndex = this.getSettings().getInt("selected_resolution", 0);
		if (selectedIndex <= adapter.getCount()) {
			spinner.setSelection(selectedIndex);
		}
		
	}
	
	public void setCheckBox() {
		CheckBox checkBox = (CheckBox) findViewById(R.id.manual_resolution);
		checkBox.setChecked(this.useManualResolution());
	}
	
	public void checkBanners() {
		if (this.isBannersEnabled()) {
			ImageView image = (ImageView) findViewById(R.id.banner_image_status);
	        image.setImageResource(R.drawable.bannerstatusenable);
	        Button b = (Button) findViewById(R.id.disableBanners);
			b.setText("Disable Banners");
		} else {
			Button b = (Button) findViewById(R.id.disableBanners);
			b.setText("Enable Banners");
			ImageView image = (ImageView) findViewById(R.id.banner_image_status);
	        image.setImageResource(R.drawable.bannerstatusdisable);
		}
	}
}
